package com.weihe.toolkit.object;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.commons.lang.StringUtils;

public class ObjectToolkit {
	
	public static Object[] getObjectAtrributValues(Object object,String attributNames){
		if(StringUtils.isNotBlank(attributNames)&&null!=object){
			String[] atattributes = attributNames.split(",");
			return getObjectAtrributValues(object,atattributes);
		}
		return null;
	}
	public static Object[] getObjectAtrributValues(Object object,String[] attributeNames){
		Object[] values = null;
		if(null != attributeNames && attributeNames.length>0 && null!=object){
			values = new Object[attributeNames.length];
			for(int i=0;i<attributeNames.length;i++){
				values[i] = getObjectField(object, attributeNames[i]);
			}
		}
		return values;
	}
	
	public static Object getObjectField(Object object, String attributeName){
		Class classType = object.getClass();
		Field fields[] =classType.getDeclaredFields();
		for(int k=0;k<fields.length;k++){
			Field field = fields[k];
			String fieldName = field.getName();
			if(StringUtils.equals(attributeName, fieldName)){
				String firstLetter = fieldName.substring(0,1).toUpperCase();
				String getMethodName = "get"+firstLetter+fieldName.substring(1);
				try {
					Method getMethod = classType.getMethod(getMethodName,new Class[]{});
					return getMethod.invoke(object,new Object[]{});
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	/**
     * 获取当前类声明的private/protected变量
     */
	public static Object getFieldValue(Object object, String propertyName)
            throws IllegalAccessException, NoSuchFieldException {
        Field field = null;
		Class<?> clazz = object.getClass();
		field = clazz.getDeclaredField(propertyName);
		field.setAccessible(true);
		return field.get(object);
    }
	/*public static Object getFieldValue(Object object, String propertyName)
	throws IllegalAccessException, NoSuchFieldException {
		Field field = null;
		Class<?> clazz = object.getClass();
		while(clazz != Object.class){
			try {
				field = clazz.getDeclaredField(propertyName);
				break;
			} catch (Exception e) {
				clazz = clazz.getSuperclass();
			}
		}
		if(null != field){
			field.setAccessible(true);
			return field.get(object);
		}
		throw new NoSuchFieldException("BeanReflectionUtils of "+propertyName);
	}*/
	
    /**
     * 因为getFieldValue()方法，无法读取super class的属性的值；
     * 					所以本方法做出扩展，允许读取super class的属性的值；
     * @param object
     * @param propertyName
     * @return
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    @SuppressWarnings("rawtypes")
	public static Object getFieldValueInAllSuper(Object object, String propertyName)
            throws IllegalAccessException, NoSuchFieldException {
        Class claszz=object.getClass();
        Field field = null;
        do
        {
        	try{
        		field = claszz.getDeclaredField(propertyName);
        	}
        	catch(NoSuchFieldException e)
        	{
        		field=null;
        	}
    		claszz=claszz.getSuperclass();
        }while(field==null && claszz!=null);
    	
    	if(field==null) return null;
 
        field.setAccessible(true);
        return field.get(object);
    }
}
